package com.sihai.ehr.web.action.exam;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.model.biz.*;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.ExaminePeriodEnum;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.model.sys.User;
import com.sihai.ehr.service.*;
import com.sihai.ehr.utils.PeriodUtils;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.Iterator;
import java.util.List;

/**
 * @creator wxn
 * @since 2012-10-27 15:00
 * 定量考核数据审核
 */
@Results({
        @Result(name = "main", location = "exam_quan_audit_main.jsp"),
        @Result(name = "audit_history", location = "exam_quan_audit_history.jsp"),
        @Result(name = "list_audit", location = "exam_quan_audit_list.jsp"),
        @Result(name = "audit_detail", location = "exam_quan_audit_detail.jsp")
})
public class ExamQuanAuditAction extends EHRAction{
    @BeanInject
    private OrganizationService organizationService;
    @BeanInject
    private ExamQuanDataInputService examQuanDataInputService;
    @BeanInject
    private EmployeeService employeeService;

    private List<Organization> organizations;

    private String examPeriodStr; //2012年5月, 2012年1~3月
    private ExaminePeriodEnum examPeriodEnum;//月，双朋，季，半年，年

    private EmployeeinfoWork work;
    private LimitedList<ExamQuanScoreDetail> examQuanScoreDetails;
    private List<ExamQuanScoreAuditing> examQuanScoreAuditings;

    private Long quanScoreDetailId;

    private int dataTotal;

    private AuditStatus status;

    private String opType;

    //量化数据审核主界面
    @Action("/exam/quan_audit_main")
    @Override
    public String execute() throws Exception {
        listAction = "/exam/quan_audit_main.xhtml";
        listTitle = "量化数据审核";
        organizations = organizationService.findChildren(null, true, null, null);
        if (organizations != null && organizations.size() > 0) {
        	if(getCurrentUserId() > 0){
            	Long topOrgId = getCurrentUser().getWork().getOrganization().getTopOrganization().getId();
            	Iterator<Organization> it = organizations.iterator();
            	while(it.hasNext()){
            		Organization o = it.next();
            		if(o.getId() != topOrgId){
            			it.remove();
            		}
            	}
            }
            organizationId = organizations.get(0).getId();
        }
        return "main";
    }
    //未审核
    @Action("/exam/quan_un_audit_list")
    public String listUnAudit()throws Exception{
        //status = AuditStatus.wait_audit;
        status = AuditStatus.within_audit;
        list();
        this.setPageUrl("exam/quan_un_audit_list.xhtml");
        return "list_audit";
    }
    //已审核
    @Action("/exam/quan_has_audit_list")
    public String listAudit()throws Exception{
        status = AuditStatus.pass_audit;
        list();
        this.setPageUrl("exam/quan_has_audit_list.xhtml");
        return "list_audit";
    }
    private String list()throws Exception{
        User user = getCurrentUser();
        if(user == null || user.getId() == null || user.getId() <= 0){
            return execute();
        }
        ExamEmbedPeriod embedPeriod = PeriodUtils.createEmbedPeriod(examPeriodStr, examPeriodEnum);
        if(embedPeriod == null){
            return "list_audit";
        }
        examQuanScoreDetails = examQuanDataInputService.searchExamQuanDetails("audit", user, organizationId, null, embedPeriod, status, createLimit(), createSorter());
        if(examQuanScoreDetails != null){
            dataTotal = examQuanScoreDetails.size();
        }
        /* 分页 */
        this.paging(examQuanScoreDetails);
        /* 设置翻页action */

        return "list_audit";
    }

    //跳转到审核页面
    @Action("/exam/quan_audit_detail")
    public String toAuditPage() throws Exception{
        User user = getCurrentUser();
        if(user == null || user.getId() == null || user.getId() <= 0){
            return execute();
        }
        ExamEmbedPeriod period = PeriodUtils.createEmbedPeriod(examPeriodStr, examPeriodEnum);
        if(period == null){
            return execute();
        }
        if(work == null || work.getId() == null){
            return execute();
        }
        work = employeeService.getEmployeeinfoWork(work.getId());
        if(status == AuditStatus.wait_audit){
            status = AuditStatus.within_audit;
        }
        //TODO:判断当前登录用户是否可审核该数据
        examQuanScoreDetails = examQuanDataInputService.searchExamQuanDetails("audit", user, organizationId, work.getId(), period, status, null, null);
        opType = "audit";

        if(examQuanScoreDetails != null && !examQuanScoreDetails.isEmpty()){
            quanScoreDetailId = examQuanScoreDetails.get(0).getId();
        }

        return "audit_detail";
    }

    //审核
    @Action("/exam/quan_audit")
    public String doAudit() throws Exception{
        String auditContent = getParameter("auditContent");
        if(quanScoreDetailId == null || status == null || StringUtils.isBlank(auditContent)){
            return execute();
        }

        ExamQuanScoreDetail quanScoreDetail = examQuanDataInputService.getExamQuanScoreDetail(quanScoreDetailId);
        if(quanScoreDetail != null){
            ExamQuanScoreAuditing audit = new ExamQuanScoreAuditing();
            audit.setAuditContent(auditContent);
            audit.setExamQuanScoreDetail(quanScoreDetail);
            audit.setStatus(status);
            audit.setLast(status == AuditStatus.pass_audit ? true : false);
            examQuanDataInputService.saveQuanAuditing(audit);
        }
        return execute();
    }

    //审核历史记录
    /*@Action(value = "/exam/quan_audit_history", results = {
            @Result(name = "success", type = "json", params = {"root", "examQuanScoreAuditings"})
    })*/
    @Action("/exam/quan_audit_history")
    public String auditHistory() throws Exception{
        if(quanScoreDetailId != null && quanScoreDetailId > 0){
            examQuanScoreAuditings = examQuanDataInputService.findExamQuanScoreAudits(quanScoreDetailId);
        }
        //return SUCCESS;
        return "audit_history";
    }

    public int getDataTotal() {
        return dataTotal;
    }

    public LimitedList<ExamQuanScoreDetail> getExamQuanScoreDetails() {
        return examQuanScoreDetails;
    }

    public EmployeeinfoWork getWork() {
        return work;
    }

    public void setWork(EmployeeinfoWork work) {
        this.work = work;
    }

    public String getExamPeriodStr() {
        return examPeriodStr;
    }

    public void setExamPeriodStr(String examPeriodStr) {
        this.examPeriodStr = examPeriodStr;
    }

    public List<Organization> getOrganizations() {
        return organizations;
    }

    public ExaminePeriodEnum getExamPeriodEnum() {
        return examPeriodEnum;
    }

    public void setExamPeriodEnum(ExaminePeriodEnum examPeriodEnum) {
        this.examPeriodEnum = examPeriodEnum;
    }

    public String getOpType() {
        return opType;
    }

    public void setOpType(String opType) {
        this.opType = opType;
    }

    public List<ExamQuanScoreAuditing> getExamQuanScoreAuditings() {
        return examQuanScoreAuditings;
    }

    public AuditStatus getStatus() {
        return status;
    }

    public void setStatus(AuditStatus status) {
        this.status = status;
    }

    public Long getQuanScoreDetailId() {
        return quanScoreDetailId;
    }

    public void setQuanScoreDetailId(Long quanScoreDetailId) {
        this.quanScoreDetailId = quanScoreDetailId;
    }

}
